﻿/// <reference path="../MicrosoftAjax.Debug.js" />

Type.registerNamespace('ExtJSExtender.Controls');

ExtJSExtender.Controls.ButtonExtenderBehavior = function (element) {
    ExtJSExtender.Controls.ButtonExtenderBehavior.initializeBase(this, [element]);
}

ExtJSExtender.Controls.ButtonExtenderBehavior.prototype = {
    _ExtControl: null,
    _TriggerPostBackEvents: null,
    initialize: function () {
        ExtJSExtender.Controls.ButtonExtenderBehavior.callBaseMethod(this, 'initialize');
        var id = this.get_element().id;
        var self = this;
        var cfg = {
            renderTo: id,
            disabled: !self.get_Enabled(),
            text: self.get_Text(),
            border: this.get_Border(),
            handler: Function.createDelegate(self, self.raiseClicked)
        };
        if (this.get_Height()) cfg.height = this.get_Height();
        if (this.get_Width()) cfg.width = this.get_Width();

        //绘制控件
        Ext.onReady(function () {
            self._ExtControl = Ext.create('Ext.Button', cfg);
        }, this);
    },
    dispose: function () {
        ExtJSExtender.Controls.ButtonExtenderBehavior.callBaseMethod(this, 'dispose');
    },
    get_Text: function () {
        return this._Text;
    },
    set_Text: function (value) {
        this._Text = value;
    },
    get_Message: function () {
        return this._Message;
    },
    set_Message: function (value) {
        this._Message = value;
    },
    get_CausesValidation: function () {
        return this._CausesValidation;
    },
    set_CausesValidation: function (value) {
        this._CausesValidation = value;
    },
    get_OnClientClick: function () {
        return this._OnClientClick;
    },
    set_OnClientClick: function (value) {
        this._OnClientClick = value;
    },
    get_Enabled: function () {
        return this._Enabled;
    },
    set_Enabled: function (value) {
        this._Enabled = value;
    },
    get_Width: function () {
        return this._Width;
    },
    set_Width: function (value) {
        this._Width = value;
    },
    get_Height: function () {
        return this._Height;
    },
    set_Height: function (value) {
        this._Height = value;
    },
    get_Border: function () {
        return this._Border;
    },
    set_Border: function (value) {
        this._Border = value;
    },
    get_DisableAfterClick: function () {
        return this._DisableAfterClick;
    },
    set_DisableAfterClick: function (value) {
        this._DisableAfterClick = value;
    },
    get_UniqueID: function () {
        return this._UniqueID;
    },
    set_UniqueID: function (value) {
        this._UniqueID = value;
    },
    get_TriggerPostBackEvents: function () {
        return this._TriggerPostBackEvents;
    },
    set_TriggerPostBackEvents: function (value) {
        this._TriggerPostBackEvents = value;
    },
    invoke: function (args, onComplete$delegate, context, onError$delegate, async) {
        if (typeof (Page_ClientValidate) == 'function' && this.get_CausesValidation() == true) {
            var valid = Page_ClientValidate();
            if (!valid) {
                return;
            }
        }

        var callbackId = this.get_UniqueID();
        __doPostBack(callbackId, args);
    },
    raiseClicked: function () {
        var message = this.get_Message();
        if (message != null && message != "") {
            if (!confirm(message)) return;
        }

        if (this.get_DisableAfterClick()) {
            this._ExtControl.disable();
        }

        var clientClick = this.get_OnClientClick();
        var clientClickFn = ExtJSExtender.Util.getFn(clientClick);
        var rst = clientClickFn.call(this);
        if (rst !== false && this._TriggerPostBackEvents.Click) {
            this.invoke("click", null, this, this.onCallbackError, true);
        }
        else {
            this._ExtControl.enable();
        }
    }
}

ExtJSExtender.Controls.ButtonExtenderBehavior.registerClass('ExtJSExtender.Controls.ButtonExtenderBehavior', Sys.UI.Control);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();